/********************************************************************************
*Class:  AddMonthlyCooperationRecordControl
*Method: NewRecord(),Save()
*Useage: Control page AddMonthlyCooperationRecord, user can create Monthly Cooperation Plan on this page.
*Notice: If the record existed, this page will give a warning "******已存在".
********************************************************************************/

public without sharing class AddMonthlyCooperationRecordControl{
    public List<AddMonthlyCooperationRecordItemControl> RecordList{get;set;}
    public List<Hospital__c> HospitalList{get;set;}
    public String msg{get;set;}
    public String msg1{get;set;}
    public List<monthly_cooperation_plan_with_hospital__c> mcpwhList{get;set;}
    public String yearSelect{get;set;}
    public String monthSelect{get;set;}
    public Integer IyearCom;
    public  Integer Imonth;
    
    public List<SelectOption> YearList
    {
        get
        {
            if(YearList== null)
                YearList= new List<SelectOption>();
            else
                YearList.clear();
            YearList.add(new SelectOption('0','无'));
            YearList.add(new SelectOption('1','2010'));
            YearList.add(new SelectOption('2','2011'));
            YearList.add(new SelectOption('3','2012'));
            YearList.add(new SelectOption('4','2013'));
            YearList.add(new SelectOption('5','2014'));
            YearList.add(new SelectOption('6','2015'));
            YearList.add(new SelectOption('7','2016')); 
            
            return YearList;                      
        }
        set;
    }
    public List<SelectOption> MonthList
    {
        get
        {
            if(MonthList== null)
                MonthList= new List<SelectOption>();
            else
                MonthList.clear();
            
            if(yearSelect != null){
                MonthList.add(new SelectOption('0','无'));
                MonthList.add(new SelectOption('1','1'));
                MonthList.add(new SelectOption('2','2'));
                MonthList.add(new SelectOption('3','3'));
                MonthList.add(new SelectOption('4','4'));
                MonthList.add(new SelectOption('5','5'));
                MonthList.add(new SelectOption('6','6'));
                MonthList.add(new SelectOption('7','7')); 
                MonthList.add(new SelectOption('8','8'));
                MonthList.add(new SelectOption('9','9'));
                MonthList.add(new SelectOption('10','10'));
                MonthList.add(new SelectOption('11','11'));
                MonthList.add(new SelectOption('12','12'));
            }
            else{
                MonthList.add(new SelectOption('0',''));
            }
                        
            return MonthList;                      
        }
        set;
    }       

    public String UrlId
    {
        get{        
            return System.currentPageReference().getParameters().get('id');     
           }  
        set;
    }
    
    public PageReference actionSupport(){

        if(RecordList==null)
            RecordList=new List<AddMonthlyCooperationRecordItemControl>();
        else     
            RecordList.clear();
            msg1 = null;
            date DtodayCom = date.today();
            
            if(yearSelect == '1'){
                IyearCom = 2010;
            }
            if(yearSelect == '2'){
                IyearCom = 2011;
            }            
            if(yearSelect == '3'){
                IyearCom = 2012;
            }
            if(yearSelect == '4'){
                IyearCom = 2013;
            }             
            if(yearSelect == '5'){
                IyearCom = 2014;
            }
            if(yearSelect == '6'){
                IyearCom = 2015;
            }  
            if(yearSelect == '7'){
                IyearCom = 2016;
            }
            if(yearSelect == '0'){
                msg= null;
                return null;
            }  
            if(monthSelect == '0'){
                msg= null;
                return null;
            }            
            if(monthSelect == '1'){
                Imonth = 1;
            }
            if(monthSelect == '2'){
                Imonth = 2;
            }
            if(monthSelect == '3'){
                Imonth = 3;
            }
            if(monthSelect == '4'){
                Imonth = 4;
            }            
            if(monthSelect == '5'){
                Imonth = 5;
            }
            if(monthSelect == '6'){
                Imonth = 6;
            }
            if(monthSelect == '7'){
                Imonth = 7;
            }
            if(monthSelect == '8'){
                Imonth = 8;
            }              
            if(monthSelect == '9'){
                Imonth = 9;
            }
            if(monthSelect == '10'){
                Imonth = 10;
            }
            if(monthSelect == '11'){
                Imonth = 11;
            }
            if(monthSelect == '12'){
                Imonth = 12;
            }
            
            if((IyearCom > date.today().year())||((IyearCom == date.today().year())&&(Imonth >= date.today().month()))){
                msg = '不能记录本月及以后的月度合作计划';
                return null;
            } 
            else{
                msg= null;
            }                                   


        //create the record items       
        Boolean haveRecord = false;
        //create the record name              

        String Smonth = String.valueOf(Imonth);
        String Syear = String.valueOf(IyearCom);
        if((IyearCom > date.today().year())||((IyearCom == date.today().year())&&(Imonth > date.today().month()))){
            msg='不能记录未来的合作计划';
            return null;        
        }
        else{
            msg= null;
        }
            
        List<monthly_cooperation_plan_with_hospital__c> haveRecordList = [select Hospital__c,hospital_class__c,
                                                                               FlagHospitalTrue__c,EFM_Rotation_True__c,DisplayTrue__c,Name,
                                                                               Id,DaysOfRotationTrue__c,CooperationPlanDone__c from 
                                                                               monthly_cooperation_plan_with_hospital__c
                                                                               where OwnerId  =: UserInfo.getUserId() and Month__c =: Imonth and Year__c =: Syear];        
        //create the record items       
        if(haveRecordList.size() <= 0){
            msg='本月无合作计划';
            return null;
        }
            for(monthly_cooperation_plan_with_hospital__c existRecord : haveRecordList)
            {
                AddMonthlyCooperationRecordItemControl existItem = new AddMonthlyCooperationRecordItemControl (existRecord);

                                    
                        existItem.FhIsCheck = existRecord.FlagHospitalTrue__c;
                        existItem.SsyIsCheck = existRecord.EFM_Rotation_True__c;
                        existItem.DisIsCheck = existRecord.DisplayTrue__c;
                        existItem.DorIsCheck = existRecord.DaysOfRotationTrue__c;
                        existItem.HosString = existRecord.name.substring(0,existRecord.name.indexOf('201'));                    
                        RecordList.add(existItem);
                        haveRecord = true;
            }                                                                                      
            return null;               
    } 
   
    public PageReference Save()
    {
        Boolean fHaveRecord = false; //set flag
      
        if(RecordList != null)
        {
            List<monthly_cooperation_plan_with_hospital__c> planNameList = [select Name,Id from monthly_cooperation_plan_with_hospital__c
                                                                                where OwnerId  =: UserInfo.getUserId()];
            List<monthly_cooperation_plan_with_hospital__c> UpdateList = new List<monthly_cooperation_plan_with_hospital__c>();
                                                                                      
            for(AddMonthlyCooperationRecordItemControl mcpwh:RecordList) //loop from item list
            {
                if(mcpwh.FhIsCheck == true){ //flag hospital is selected
                    mcpwh.planRecord.FlagHospitalTrue__c = true;
                    mcpwh.planRecord.CooperationPlanDone__c  = true;  
                }
                else{
                    mcpwh.planRecord.FlagHospitalTrue__c = false;             
                }                            
                if(mcpwh.SsyIsCheck == true){ //SSY center is selected
                    mcpwh.planRecord.EFM_Rotation_True__c = true;
                    mcpwh.planRecord.CooperationPlanDone__c  = true;  
                }
                else{
                    mcpwh.planRecord.EFM_Rotation_True__c = false;                
                }
                if(mcpwh.DisIsCheck == true){ //Display is selected
                    mcpwh.planRecord.DisplayTrue__c = true;             
                    mcpwh.planRecord.CooperationPlanDone__c  = true;  
                }
                else{
                    mcpwh.planRecord.DisplayTrue__c = false;                     
                }
                
                if(mcpwh.DorIsCheck == true){ //days of rotation is keyed in.
                    mcpwh.planRecord.DaysOfRotationTrue__c = true;                    
                   mcpwh.planRecord.CooperationPlanDone__c  = true;  
                }
                else{
                    mcpwh.planRecord.DaysOfRotationTrue__c = false;                    
                              
                }
                UpdateList.add(mcpwh.planRecord);
             }
            update UpdateList;
             msg1 = '保存成功';
            //return page to Monthly Cooperation Plan            
            return null;
        }
        return null;
    }
    
    static testMethod void AddMonthlyCooperationRecordControlTest()
    {
        Profile p  = [select id from profile where name = 'System Administrator'or name =: '系统管理员'];
        
        User u1 = new User (alias = 'standt', email = 'shuaicao@isoftstone.com', emailencodingkey='UTF-8', 
                lastname = 'Testing', languagelocalekey = 'en_US', localesidkey='en_US', Title__c = 'Sales Rep',profileid = p.id, 
                timezonesidkey = 'America/Los_Angeles', username='standarduser@testorg1.com');
        insert u1;
        
        Hospital__c hos = new Hospital__c();
        hos.Name = 'aaaaaaaa';
        hos.Active__c = true;
        hos.Nickname__c = 'aaaaaaaaaaa';
        hos.OwnerId = u1.Id;
        insert hos;    
    
        System.runAs(u1) {    
            monthly_cooperation_plan_with_hospital__c add = new monthly_cooperation_plan_with_hospital__c();
            add.Hospital__c = hos.Id;
            insert add;
    
            AddMonthlyCooperationRecordControl testCase = new AddMonthlyCooperationRecordControl();
            System.assert(testCase.UrlId == null);
            
            
            testCase.RecordList = new List<AddMonthlyCooperationRecordItemControl>();
            
            testCase.RecordList = null;
            testCase.Save();                
            
            testCase.RecordList = new List<AddMonthlyCooperationRecordItemControl>();

            
            AddMonthlyCooperationRecordItemControl addr = new AddMonthlyCooperationRecordItemControl(add);
            testCase.RecordList.add(addr);
           
            testCase.RecordList.get(0).FhIsCheck  = true;
            testCase.RecordList.get(0).SsyIsCheck = true;
            testCase.RecordList.get(0).DisIsCheck = true;
            testCase.RecordList.get(0).DorIsCheck = true;
            
            testCase.Save();
        }
    
    }
}